package red.stu.pconstraint.scanner;

import org.jetbrains.annotations.NotNull;
import red.stu.pconstraint.util.PackageStringUtils;

import java.io.File;
import java.util.List;
import java.util.regex.Matcher;

/**
 * Class扫描器
 * @author yihrmc @stu.red
 * @date 2019/11/18 12:56
 */
public class ClassScanner extends ResourceScanner<String> {

    private static final String FILE_SUFFIX = ".class";

    @Override
    public List<String> scan(@NotNull String packageName) {
        String scanPath = PackageStringUtils.packageToPath(packageName);
        return super.scan(scanPath);
    }

    @Override
    protected String handleScanFile(String filePath) {
        if (filePath.endsWith(FILE_SUFFIX)) {
            String className = PackageStringUtils.pathToPackage(trimFileSuffix(filePath));
            return className;
        }
        return null;
    }

    private static String trimFileSuffix(String fileName) {
        int i = fileName.lastIndexOf('.');
        if (i == -1) {
            return fileName;
        }
        return fileName.substring(0, i);
    }

}
